package mao.algorithm.redbook.demo29_9;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;



//断点调试
public class TestTree {
	public static void main(String[] args) throws FileNotFoundException {
		File file = new File("D:testWeightedGraph.txt");
		FileReader fis = new FileReader(file);
		BufferedReader in = new BufferedReader(fis);
		
		try {
			int read = Integer.parseInt(in.readLine().trim());
			WeightedGraph graph = new WeightedGraph(read);
			int u=0;
			int v=0;
			int weight=0;
			for(int i=0;i<read-1;i++){ //行数小于节点个数
				String line = in.readLine();
				String[] split = line.split("\\|");
				for(int j=0;j<split.length;j++){
//					u = Integer.parseInt(split[j].split(" ")[0].trim());
//					v=Integer.parseInt(split[j].split(" ")[1].trim());
//					weight=Integer.parseInt(split[j].split(" ")[2].trim());
//					graph.addEdge(u, v, weight);
					String[] number = split[j].split(" ");
					int count=0;
					int[] number_int = new int[3];
					int index = 0;
					while(count < 3){
						if(number[index] != null && !number[index].trim().equals("")){
							number_int[count]=Integer.parseInt(number[index].trim());
							count++;
						}
						index++;
					}
					u = number_int[0];
					v=number_int[1];
					weight=number_int[2];
					graph.addEdge(u, v, weight);
				}
				
			}
		    graph.printEdges();
			WeightedGraph.WeightTree tree = graph.getMinmunSpaningTree(0);
			tree.printTotalWeight();
			tree.printTree();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
